/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tree;

/**
 *
 * @author Ej-Dý
 */
/**
 * Vnitrni forma: identifikator.
 */
public class IdentifierTree extends VyrazTree {

    private String name;
    private VariableTree variable;
    private Type previous;
    private Type now;
   

    /**
     * Je identifikator na leve strane prirazeni?
     */
    private boolean leftValue;

    public IdentifierTree(Position start, Position end, String name) {
        super(start, end);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public VariableTree getVariable() {
        return variable;
    }

   

    public void setVariable(VariableTree variable) {
        this.variable = variable;
    }

    public boolean isLeftValue() {
        return leftValue;
    }

    public void setLeftValue(boolean leftValue) {
        this.leftValue = leftValue;
    }

    public int getSlot() {
        return variable.getSlot();
    }



    @Override
    public void accept(TreeVisitor visitor) {
        visitor.visitIdentifier(this);
    }

    @Override
    public void generate(Context ctx) {
        switch (variable.getType()) {
        case ARRAY:
            ctx.print("a");
            break;
            case STRING:
        case INTEGER:
            //ctx.print("i");
            break;
        case REAL:
            ctx.print("d");
            break;
        case CONV:
            //ctx.print("s");
            variable.setType(previous);
            break;
        }
        ctx.println("load " + variable.getSlot());
        if(now==Type.CONV){
            ctx.println("convert");
            now=previous;
        }
    }

    @Override
    public String toString() {
        return String.format("Identifier %s: '%s'", super.toString(), name);
    }

    
    public Type getType() {
        return variable.getType();
    }

    @Override
    public void setType(Type t) {
        previous=variable.getType();
        now=t;
        
    }
}
